package com.fs.dao.Impl;

import com.fs.dao.ClassStudentDao;
import com.fs.dao.CourseDao;
import com.fs.pojo.Class;
import com.fs.pojo.Course;
import com.fs.pojo.Paper;
import com.fs.utils.BaseContext;
import com.fs.utils.IdWorker;
import com.fs.utils.JDBCUtils;

import java.util.List;

public class CourseDaoImpl implements CourseDao {

    @Override//根据id查询课程信息
    public Course queryById(String id) {
        String sql = "select * from tb_course where id = ?";
        List<Course> courses = JDBCUtils.executeQurey(sql, Course.class,id);
        return courses == null || courses.isEmpty() ? null:courses.get(0);
    }

    @Override//查询所有课程信息
    public List<Course> queryAll() {
        String sql = "select * from tb_course";
        return JDBCUtils.executeQurey(sql, Course.class);
    }

    @Override//添加课程信息
    public int insert(Course obj) {
        String sql = "INSERT into tb_course (id,teacher_id, course_name, img, course_description) values (?,?,?,?,?)";
        int index = JDBCUtils.executeUpdate(sql,obj);
        return index;
    }


    @Override//删除课程信息
    public int deleteById(String id) {
        String sql = "delete from tb_course where id = ?";
        return JDBCUtils.executeUpdate(sql,id);
    }

    @Override//更新课程信息
    public int update(Course obj) {
        String sql = "update tb_course c set c.course_name = ? ,c.img = ?,c.course_description = ? where c.id = ?";

        return JDBCUtils.executeUpdate(sql,obj.getCourseName(),obj.getImg(),obj.getCourseDescription(),obj.getId());
    }

    @Override
    public Course QueryAllByCourseName(String courseName) {
        String sql = "select * from tb_course where course_name = ?";
        List<Course> courses = JDBCUtils.executeQurey(sql, Course.class, courseName);
        return courses == null ? null : courses.get(0);
    }

    public void savePaper(){


    }

    @Override
    public List<Course> queryCoursesByTeacherId(String teacherId) {
        String sql = "select * from tb_course where  teacher_id = ?";

        return JDBCUtils.executeQurey(sql,Course.class,teacherId);
    }

    @Override
    public String insertReturnId(Course course) {

        IdWorker idWorker =  new IdWorker();
        String id = String.valueOf(idWorker.nextId());
        course.setId(id);

        String sql = "INSERT INTO tb_course(id, teacher_id, course_name, img, course_description) values(?,?,?,?,?) ";


        return JDBCUtils.executeUpdate(sql,course.getId(),course.getTeacherId(),course.getCourseName(),course.getImg(),course.getCourseDescription())>0
                ?id:null;
    }

}
